package com.marketfarm.client;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.marketfarm.client.CachePagina.Pagina;
import com.marketfarm.client.footer.Footer;
import com.marketfarm.client.service.AbstractAsynCallback;
import com.marketfarm.client.service.UsuarioService;
import com.marketfarm.client.service.UsuarioServiceAsync;
import com.marketfarm.shared.Utils;

public class Login extends Composite {
	private TextBox txbUsuario;
	private PasswordTextBox txbSenha;

	public Login() {
		
		FlowPanel flowPanel = new FlowPanel();
		initWidget(flowPanel);
		flowPanel.setSize("", "");
		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setSpacing(10);
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.setSize("", "");
		flowPanel.add(verticalPanel);
		
		Header header = new Header();
		verticalPanel.add(header);
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setSpacing(10);
		verticalPanel.add(horizontalPanel);
		
		DecoratorPanel decoratorPanel_1 = new DecoratorPanel();
		horizontalPanel.add(decoratorPanel_1);
		
		VerticalPanel verticalPanel_5 = new VerticalPanel();
		decoratorPanel_1.setWidget(verticalPanel_5);
		verticalPanel_5.setWidth("387px");
		
		FlowPanel flowPanel_1 = new FlowPanel();
		verticalPanel_5.add(flowPanel_1);
		flowPanel_1.setSize("553px", "254px");
		
		HorizontalPanel horizontalPanel_3 = new HorizontalPanel();
		horizontalPanel_3.setSpacing(10);
		flowPanel_1.add(horizontalPanel_3);
		
		Image image = new Image("imgs/capa.jpg");
		horizontalPanel_3.add(image);
		image.setSize("203px", "180px");
		
		Label lblNewLabel_1 = new Label("O Market-farm é um jogo de mercado. " +
				"Seu objetivo é enriquecer. " +
				"Para isso existem duas opções. " +
				"Trabalhar ou especular. " +
				"Comece uma produção e vá ao mercado o mais cedo possível. " +
				"Adquira um pouco de dinheiro e comece com a sua estratégia de especulação. " +
				"Tome cuidado com os eventos externos. " +
				"Não morra de fome e seja feliz. " +
				"Boa sorte.");
		lblNewLabel_1.setStyleName("font-16");
		horizontalPanel_3.add(lblNewLabel_1);
		lblNewLabel_1.setSize("317px", "173px");
		
		Label lblLoremIpsum = new Label("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an");
		lblLoremIpsum.setStyleName("padding-left10");
		flowPanel_1.add(lblLoremIpsum);
		lblLoremIpsum.setSize("512px", "64px");
		
		VerticalPanel verticalPanel_1 = new VerticalPanel();
		horizontalPanel.add(verticalPanel_1);
		
		DecoratorPanel decoratorPanel_2 = new DecoratorPanel();
		verticalPanel_1.add(decoratorPanel_2);
		
		VerticalPanel verticalPanel_3 = new VerticalPanel();
		verticalPanel_3.setSpacing(10);
		decoratorPanel_2.setWidget(verticalPanel_3);
		
		HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		horizontalPanel_1.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		verticalPanel_3.add(horizontalPanel_1);
		
		Label lblUsuario = new Label("Usu\u00E1rio:");
		horizontalPanel_1.add(lblUsuario);
		lblUsuario.setWidth("54px");
		
		final Button btnLogin = new Button("Entrar");
		txbUsuario = new TextBox();
		txbUsuario.addKeyPressHandler(new KeyPressHandler() {
			public void onKeyPress(KeyPressEvent event) {
				if(KeyCodes.KEY_ENTER == event.getNativeEvent().getKeyCode()) btnLogin.click();
			}
		});
		horizontalPanel_1.add(txbUsuario);
		txbUsuario.setWidth("148px");
		
		HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		horizontalPanel_2.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		verticalPanel_3.add(horizontalPanel_2);
		
		Label lblSenha = new Label("Senha:");
		horizontalPanel_2.add(lblSenha);
		lblSenha.setWidth("54px");
		
		txbSenha = new PasswordTextBox();
		txbSenha.addKeyPressHandler(new KeyPressHandler() {
			public void onKeyPress(KeyPressEvent event) {
				if(KeyCodes.KEY_ENTER == event.getNativeEvent().getKeyCode()) btnLogin.click();
			}
		});
		horizontalPanel_2.add(txbSenha);
		txbSenha.setWidth("148px");
		
		HorizontalPanel horizontalPanel_4 = new HorizontalPanel();
		horizontalPanel_4.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		verticalPanel_3.add(horizontalPanel_4);
		horizontalPanel_4.setWidth("210px");
		
		btnLogin.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(Utils.isEmpty(txbSenha.getText()) || Utils.isEmpty(txbUsuario.getText())){
					Window.alert("Preencha o usuário e a senha");
				}
				else{
					btnLogin.setEnabled(false);
					
					UsuarioServiceAsync service = GWT.create(UsuarioService.class);
					service.login(txbUsuario.getText(), txbSenha.getText(), new Date().getTime(), new AbstractAsynCallback<String>(){
						@Override
						public void onSuccess(String sessionID) {
							if(sessionID == null) Window.alert("Usuário ou senha inválido");
							else{
								final long DURATION = 1000 * 60 * 60 * 24; //1 dia.
							    Date expires = new Date(System.currentTimeMillis() + DURATION);
							    Cookies.setCookie("sid", sessionID, expires, null, "/", false);
							    
							    History.newItem(Pagina.HOME.toString());
							}
							
							btnLogin.setEnabled(true);
						}
						
						@Override
						public void onFailure(Throwable caught) {
							super.onFailure(caught);
							btnLogin.setEnabled(true);
						}
					});
				}
			}
		});
		horizontalPanel_4.add(btnLogin);
		btnLogin.setWidth("78px");
		
		HorizontalPanel horizontalPanel_5 = new HorizontalPanel();
		verticalPanel_3.add(horizontalPanel_5);
		
		Hyperlink hprlnkEsqueciMinhaSenha = new Hyperlink("Esqueci minha senha", false, "newHistoryToken");
		horizontalPanel_5.add(hprlnkEsqueciMinhaSenha);
		
		SimplePanel simplePanel = new SimplePanel();
		verticalPanel_1.add(simplePanel);
		simplePanel.setHeight("10px");
		
		DecoratorPanel decoratorPanel_3 = new DecoratorPanel();
		verticalPanel_1.add(decoratorPanel_3);
		
		VerticalPanel verticalPanel_2 = new VerticalPanel();
		verticalPanel_2.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel_2.setSpacing(10);
		decoratorPanel_3.setWidget(verticalPanel_2);
		
		Label lblNewLabel = new Label("Criar Novo Usu\u00E1rio");
		lblNewLabel.setStyleName("font-18");
		verticalPanel_2.add(lblNewLabel);
		lblNewLabel.setWidth("210px");
		
		Button btnCadastreSeAqui = new Button("Cadastre-se aqui");
		btnCadastreSeAqui.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				History.newItem(Pagina.NOVO_USUARIO.toString());
			}
		});
		btnCadastreSeAqui.setStyleName("font-18");
		verticalPanel_2.add(btnCadastreSeAqui);
		
		Footer footer = new Footer();
		verticalPanel.add(footer);
	}

}
